<template>
	<view class="p-4 text-base">
		<view class="text-h4 mb-2">选号参数</view>
		<view class="text-base mb-4">推荐方案：【初选/7列选号池/过滤2】、【初选/前后区选号池/过滤3】、【优选/7列选号池/过滤2】、【优选/前后区选号池/过滤3】中奖率最佳。</view>
		<uni-forms :modelValue="paramFormData">
			<uni-forms-item label="备注信息">
				<uni-easyinput v-model="paramFormData.remark" placeholder="请输入备注" />
			</uni-forms-item>
			<uni-forms-item label="选号模式">
				<uni-data-select v-model="paramFormData.chooseMode"
					:localdata="ssqTplModel.ChooseModeData"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="号源类型">
				<uni-data-select v-model="paramFormData.poolType"
					:localdata="ssqTplModel.PoolTypeData"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="号池过滤">
				<uni-data-select v-model="paramFormData.filterLevel"
					:localdata="ssqTplModel.FilterLevelData"></uni-data-select>
			</uni-forms-item>
		</uni-forms>
		<view class="text-h4 my-2">复盘开奖</view>
		<view class="flex items-center justify-between">
			<view class="me-1">本期：{{activeDrawInfo.lotteryDrawNum}}</view>
			<uni-easyinput v-model="activeDrawInfo.lotteryDrawResult" placeholder="本期开奖号码" />
		</view>
		<view class="flex items-center my-1">
			<view class="me-4">上期：{{prevDrawInfo.lotteryDrawNum}}</view>
			<view>{{prevDrawInfo.lotteryDrawResult}}</view>
		</view>
		<view class="flex items-center justify-between mb-2">
			<view>复盘：</view>
			<uni-data-select :localdata="historyData" @change="onChangeHistoryData"></uni-data-select>
		</view>
		<view class="my-2">
			<button size="mini" type="primary" @click="batchGenerateOrders()">生成号码</button>
			<button size="mini" class="mx-1" @click="onReset()">重置</button>
			<button size="mini" @click="onCopy()">复制</button>
			<button size="mini" class="mx-1" @click="onSave()">暂存</button>
		</view>
		<view>
			<view class="flex items-center justify-between">
				<view>号码</view>
				<view>结果</view>
				<view>操作</view>
			</view>
			<template v-if="activeDrawInfo.lotteryDrawResult.length > 0">
				<view class="flex items-center justify-between" v-for="(item, idx) in orderInfo.drawList" :key="idx">
					<view v-html="item.text"></view>
					<view class="text-error" v-if="item.status>0">{{`${item.statusText}(${item.moneyText})`}}</view>
					<view class="text-error" v-else>{{item.statusText}}</view>
					<view><button size="mini" type="warn" @click="onDeleteRow(idx)">删除</button></view>
				</view>
			</template>
			<template v-else>
				<view class="flex items-center justify-between" v-for="(item, idx) in orderInfo.list" :key="idx">
					<view>{{item}}</view>
					<view class="text-error">-</view>
					<view><button size="mini" type="warn" @click="onDeleteRow(idx)">删除</button></view>
				</view>
			</template>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';

	import SsqDataModel from '@/model/SsqDataModel';
	import SsqOrderModel from '@/model/SsqOrderModel';
	import SsqTplModel from '@/model/SsqTplModel';
	import FsDriver from '@/model/FsDriver';
	import {
		copyText
	} from '@/utils/Utils';

	const fsDriver = new FsDriver();
	const ssqTplModel = new SsqTplModel();
	const ssqDataModel = new SsqDataModel();
	const ssqOrderModel = new SsqOrderModel();
	const dataset = [...ssqDataModel.getDataSource()];
	const weekdays=['日','一','二','三','四','五','六'];
	const historyData = dataset.map((v, i) => {
		return {
			text: `${v.lotteryDrawNum} （${v.lotteryDrawTime.substring(5)} 周${weekdays[new Date(v.lotteryDrawTime).getDay()]}）（${v.lotteryDrawResult}）`,
			value: i
		};
	});
	const prevDrawInfo = ref(dataset.length > 0 ? dataset[0] : {
		lotteryDrawNum: '',
		lotteryDrawResult: ''
	});
	const activeDrawInfo = ref({
		lotteryDrawNum: parseInt(prevDrawInfo.value.lotteryDrawNum) + 1,
		lotteryDrawResult: ''
	});

	// const tplData = ref({
	// 	...ssqTplModel.getTplForFileData({
	// 		PrevDrawalInfo: prevDrawInfo.value,
	// 		DrawalInfo: {
	// 			lotteryDrawNum: activeDrawInfo.value.lotteryDrawNum,
	// 			lotteryDrawResult: "",
	// 			lotteryDrawTime: ""
	// 		},
	// 	})
	// });

	const resetParamFormData = {
		remark: '',
		chooseMode: '初选',
		poolType: 'columns',
		filterLevel: "1",
	};
	const paramFormData = ref({
		...resetParamFormData
	});

	const orderInfo = ref({});

	const onReset = function() {
		paramFormData.value = {
			...resetParamFormData
		};
		prevDrawInfo.value = dataset.length > 0 ? dataset[0] : {
			lotteryDrawNum: '',
			lotteryDrawResult: ''
		};
		activeDrawInfo.value = {
			lotteryDrawNum: parseInt(prevDrawInfo.value.lotteryDrawNum) + 1,
			lotteryDrawResult: ''
		}
	}
	const onChangeHistoryData = function(value) {
		activeDrawInfo.value = dataset[value];
		prevDrawInfo.value = dataset[value + 1];
	}

	const batchGenerateOrders = () => {
		orderInfo.value = {
			...ssqTplModel.getTplForOrderListItem({
				param: paramFormData.value
			})
		};
		let drawResult = activeDrawInfo.value?.lotteryDrawResult?.trim();
		let prevDrawResult = prevDrawInfo.value?.lotteryDrawResult?.trim();
		let isDraw = drawResult?.length > 0;
		console.log('isDraw', isDraw, activeDrawInfo.value)
		orderInfo.value.list = [];
		orderInfo.value.drawList = [];
		onChooseAuto(10);
		if (orderInfo.value.param.chooseMode == '优选') {
			orderInfo.value.list = ssqOrderModel.getBetterNums7(orderInfo.value.list)
		}
		if (isDraw) {
			orderInfo.value.drawList = ssqOrderModel.calcOrder(drawResult,
				orderInfo.value.list, false, prevDrawResult);
		}
		console.log('orderInfo', orderInfo);
	}

	/**
	 * 机选一注
	 */
	const onChooseAuto = (count = 1) => {
		let nums = [],
			n = count == 1 ? count : (count - orderInfo.value.list.length);
		let filterLevel = parseInt(orderInfo.value.param.filterLevel);
		let poolType = orderInfo.value.param.poolType;
		while (n > 0) {
			nums = ssqOrderModel.getRandomNums7(poolType, filterLevel, prevDrawInfo.value.lotteryDrawResult);
			orderInfo.value.list.push(nums[0].join(' ') + ' ' + nums[1].join(' '));
			n--;
		}
	}

	/**
	 * 优选（在机选号码里面继续机选）
	 */
	const onChooseAutoBetter = () => {
		if (orderInfo.value.list.length == 0) {
			uni.showToast({
				title: '请先选择机选的号码',
				icon: 'error'
			})
			return;
		}
		orderInfo.value.list = ssqOrderModel.getBetterNums7(orderInfo.value.list);
	}
	const onDeleteRow = (idx) => {
		orderInfo.value.list.splice(idx, 1);
		if (orderInfo.value.drawList.length > 0) {
			orderInfo.value.drawList.splice(idx, 1);
		}
	}

	const onCopy = () => {
		copyText(orderInfo.value.list.join('\n'));
		uni.showToast({
			title: '复制成功',
			icon: 'success'
		})
	}

	const onSave = () => {
		fsDriver.save('temp-order', JSON.stringify(orderInfo.value.list));
		uni.showToast({
			title: '暂存成功，可以在【帮助中心】重新获取内容。',
			icon: 'success'
		})
	}
</script>

<style>
	:deep(.uni-forms-item) {
		margin-bottom: 4px;
	}
</style>